package com.ian.departmentmanager.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ian.departmentmanager.bo.Department;
import com.ian.departmentmanager.bo.Member;
import com.ian.departmentmanager.dao.DepartmentDao;
import com.ian.departmentmanager.dao.MemberDao;
import com.ian.departmentmanager.service.DeptService;
import com.ian.departmentmanager.service.MemberService;

public class test {

	public static void main(String[] args) {
		//insertDept();
		// insert();
		 //get();
		getlist();
		// update();
		//getMems();
		//del();
		 //updateMem();
	}

	public static void get() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		MemberDao dao = (MemberDao) ctx.getBean("memberDao");
		Member mem = dao.getMember(1);
		System.out.println(mem.getId());
	}
	
	public static void del() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		MemberDao dao = (MemberDao) ctx.getBean("memberDao");
		dao.delMember(2091458);
		System.out.println("Delete Success!");
	}

	public static void getlist() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		MemberService dao = (MemberService) ctx.getBean("memberService");
		List<Member> mems = dao.getMembers();
		System.out.println(mems.get(0).getId());
	}

	public static void insert() {


		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		MemberDao dao = (MemberDao) ctx.getBean("memberDao");
		DeptService deptService = (DeptService) ctx.getBean("deptService");
		Department dept = deptService.getDept(1);
		Member mem = new Member();
		mem.setStuName("Jason");
		mem.setStuID("1191457");
		mem.setDept(dept);
		mem.setStuProperties(1);
		dao.insertMember(mem);

	}

	public static void update() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		DepartmentDao dao = (DepartmentDao) ctx.getBean("departmentDao");
		dao.updaeDept(1, "WenYiBu");
	}
	
	public static void getMems() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		MemberService service = (MemberService) ctx.getBean("memberService");
/*		List<Member> mems = dao.getMembers(1);
		System.out.println(mems.get(0).getStuName());*/
		List<Member> mems = service.getMembers();
		System.out.println(mems.get(1).getDept().getDepartmentName());
	}
	
	public static void insertDept() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		DeptService deptService = (DeptService) ctx.getBean("deptService");
		Department dept = new Department();
		dept.setDepartmentID(2);
		dept.setDepartmentName("TiYuBu");
		deptService.insertDept(dept);
	}
	
	public static void updateMem() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"hibernate_context.xml");
		MemberDao dao = (MemberDao) ctx.getBean("memberDao");
		dao.updateMember(2091457, "KOKO", "2090457", "jj", "jj", "jj");
		System.out.println("ok");
	}

}
